<template>
  <div class='container'>
    <el-card>
      <!-- 筛选条件 -->
      <el-row type="flex">
        <el-col>
          <el-form
            lable-width="200px"
            size="small"
            :inline="true"
            :model="catalogue"
          >
            <el-form-item
              label="目录名称"
              style="padding-left:12px"
              prop="directoryName"
            >
              <el-input
                style="200px"
                v-model="catalogue.directoryName"
              ></el-input>
            </el-form-item>
            <el-form-item
              label="状态"
              style="padding-left:40px"
            >
              <el-select
                v-model="catalogue.state"
                placeholder="请选择"
              >
                <el-option
                  label="启用"
                  value="1"
                ></el-option>
                <el-option
                  label="禁用"
                  value="0"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button @click="btnClear">清除</el-button>
              <el-button
                type="primary"
                @click="btnSearch"
              >搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col justify="end">
          <el-button
            style="float: right"
            icon="el-icon-edit"
            @click="openDialog()"
            type="success"
            size="small"
          >
            新增目录
          </el-button>
          <el-button
            style="float: right; padding: 9px 12px"
            type="text"
            icon="el-icon-back"
            @click="$router.back()"
            v-if="btnBack.id && btnBack.name"
          >
            返回学科
          </el-button>
        </el-col>
      </el-row>
      <!-- 提示文本 -->
      <el-alert
        :title="`数据一共${total}条`"
        type="info"
        :closable="false"
        show-icon
        style="margin-bottom:15px"
      ></el-alert>
      <!-- 表格数据 -->
      <el-table
        :data="catalogform"
        :header-cell-style="{background:'#fafafa',borderBottom:'2px solid #e8e8e8'}"
        :cell-style="{padding:'16px 0',fontSize:'15px'}"
      >
        <el-table-column
          label="序号"
          type="index"
          width="80px"
        ></el-table-column>
        <el-table-column
          label="所属学科"
          prop="subjectName"
          width="240px"
        ></el-table-column>
        <el-table-column
          label="目录名称"
          prop="directoryName"
          width="240px"
        ></el-table-column>
        <el-table-column
          label="创建者"
          prop="username"
          width="236px"
        ></el-table-column>
        <el-table-column
          label="创建日期"
          width="240px"
        >
          <template slot-scope="scope">
            {{scope.row.addDate | parseTimeByString}}
          </template>
        </el-table-column>
        <el-table-column
          label="面试题数量"
          prop="totals"
          width="236px"
        ></el-table-column>
        <el-table-column
          label="状态"
          width="236px"
        >
          <template slot-scope="scope">
            {{scope.row.state===1? "已启用":"已禁用"}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-link
              type="primary"
              :underline=false
              @click="changedState(scope.row)"
            >{{scope.row.state===1? "禁用":"启用"}}</el-link>
            <el-link
              type="primary"
              :underline=false
              @click="openDialog(scope.row)"
              :disabled="scope.row.state === 1"
            >修改</el-link>
            <el-link
              type="primary"
              :underline=false
              :disabled="scope.row.state === 1"
              @click="btnRemove(scope.row)"
            >删除</el-link>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页数字 -->
      <el-pagination
        class="pager"
        background
        layout="prev, pager, next,sizes,jumper"
        :page-sizes=[5,10,20,50]
        :total="total"
        :page-size="catalogue.pagesize"
        :current-page="catalogue.page"
        @current-change="pageChange"
        @size-change="sizeChang"
      >
      </el-pagination>
    </el-card>
    <!-- 添加|修改 -->
    <dialog-add
      ref="refopenDialog"
      :data="directoryAdd"
      @updateList="updateList()"
    >
    </dialog-add>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/directorys'
import dialogAdd from '../components/directorys-add.vue'

export default {
  name: 'container',
  components: {
    dialogAdd
  },
  data () {
    return {
      catalogue: {
        directoryName: '', // 目录名称
        page: 1, // 页码
        pagesize: 10, // 每页条数
        state: null
      },
      total: 0, // 总条数
      catalogform: [], // 目录表格
      directoryAdd: {} // 新增数据
    }
  },
  computed: {
    btnBack () {
      return this.$route.query || {}
    }
  },
  created () {
    this.getCatalog()
  },
  methods: {
    // 获取列表数据
    async getCatalog () {
      // 调用api目录列表相关数据,结构一层data
      const { data } = await list(this.catalogue)
      this.catalogform = data.items
      this.total = data.counts
    },
    // 分页
    pageChange (newPage) {
      this.catalogue.page = newPage
      this.getCatalog()
    },
    sizeChang (newSize) {
      this.catalogue.page = 1
      this.catalogue.pagesize = newSize
      this.getCatalog()
    },
    // 清除&搜索按钮
    btnClear () {
      this.catalogue = {
        directoryName: '', // 目录名称
        page: 1, // 页码
        pagesize: 10 // 每页条数
      }
    },
    btnSearch () {
      this.catalogue.page = 1
      this.getCatalog()
    },
    // 禁用和启用的状态
    async changedState (the) {
      await changeState({
        id: the.id,
        state: the.state === 1 ? 0 : 1
      })
      this.$message.success('操作成功')
      the.state = the.state === 1 ? 0 : 1
    },

    // 弹层组件
    openDialog (add = {}) {
      this.directoryAdd = add
      this.$nextTick(() => {
        this.$refs.refopenDialog.open()
      })
    },
    // 新增和修改
    updateList () {
      if (!this.directoryAdd.id) {
        this.catalogue.page = 1
      }
      this.getCatalog()
    },
    // 删除按钮
    async btnRemove (del) {
      await this.$confirm('此操作将永久删除该标签, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await remove(del)
      this.$message.success('删除成功')
      this.getCatalog()
    }
  }
}
</script>

<style scoped lang='scss'>
.container {
  padding: 10px;
  .pager {
    margin: 15px 0;
    float: right;
  }
}
.el-table-column {
  font-size: 30px;
  line-height: 30px;
}
.el-link {
  margin-right: 10px;
}
</style>
